package com.ys.controller.web.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.service.base.TbUserMonthPerformanceReportService;
import com.ys.service.hr.TwlStockOutService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.base.TbUserMonthPerformanceReport;
import com.ys.entity.base.TbUserMonthPerformanceReportItem;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.util.DateUtil;
import com.ys.vo.W;

/**
 * 员工月度绩效申报
 */
@Controller
@RequestMapping("/web/base/tbUserMonthPerformanceReport")
public class WebTbUserMonthPerformanceReportController {
	private static final Logger logger = LoggerFactory.getLogger(WebTbUserMonthPerformanceReportController.class);

	@Autowired
	private CommonApiService<TbUserMonthPerformanceReport> commonApiService;

    @Autowired
    private TbUserMonthPerformanceReportService tbUserMonthPerformanceReportService;

    /*
     * 新增
     */
    @RequiresPermissions("tbUserMonthPerformanceReport:add")
    @GetMapping("/toAdd")
    public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {
        try {
            TbUserMonthPerformanceReport entity = new TbUserMonthPerformanceReport();
            String bid = commonApiService.getCommonNextBid(MenuId.TB_UserMonthPerformanceReport);
            entity.setBid(bid);

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TB_UserMonthPerformanceReport, new ArrayList<Map<String, Object>>());

            return commonApiService.toAdd(model, entity, MenuId.TB_UserMonthPerformanceReport, null, subGridConfig);
            //return "base/tbUserMonthPerformanceReport/toAdd";
        }catch (Exception e) {
            logger.error("新增页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
        try {
            //获取审核时间
            YsProcInst ysProcInst = new YsProcInst().setBid(bid).queryFirst();
            Map<String, Object> entity = tbUserMonthPerformanceReportService.getExtraParam(bid);

            List<Map<String, Object>> itemList = tbUserMonthPerformanceReportService.getItemList(bid);
            
            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TB_UserMonthPerformanceReport, itemList);
            Integer onProcess = ysProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value ? 0 : 1;
            model.addAttribute("onProcess", onProcess);

            return commonApiService.toEdit(model,Long.parseLong(entity.get("id").toString()), new TbUserMonthPerformanceReport(), MenuId.TB_UserMonthPerformanceReport, entity, subGridConfig);
            //return "base/tbUserMonthPerformanceReport/toEdit";
        }catch (Exception e) {
            logger.error("编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    /*
     * 详情
     */
    @GetMapping("/toDetail")
    public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
        try {
            TbUserMonthPerformanceReport entity = new TbUserMonthPerformanceReport().setBid(bid).queryFirst();
            Map<String, Object> extraParam = new HashMap<>();
            //获取审核时间
            YsProcInst ysProcInst = new YsProcInst().setBid(bid).queryFirst();
            if(ysProcInst!=null && ysProcInst.getUpdateTime()!=null)extraParam.put("auditingTime", DateUtil.getYYYY_MM_DD_HH_mm_ss(ysProcInst.getUpdateTime()));

            List<Map<String, Object>> itemList = new TbUserMonthPerformanceReportItem()
                    .addLeftJoin("TB_UserMonthPerformance M ON A.MonthPerformanceId = M.Id")//员工月度绩效信息
                    .addField("M.Rank AS rank")//岗位职级
                    .addField("M.Type AS type")//岗位工种
                    .addField("M.PointMonth AS pointMonth")//评定年月

                    .addLeftJoin("TS_UserInfo U ON U.UserId = M.UserId")//用户信息表
                    .addField("(U.UserId + '/' + U.UserName) AS userName")//工号/姓名
                    //.addField("U.DeptName AS deptName")//部门
                    .addLeftJoin("TS_OrganizationInfo OI ON OI.Orgid = U.DeptId")
                    .addField("OI.DeptName AS deptName")//部门

                    .addLeftJoin("THR_UserArchives UAS ON UAS.UserId = M.UserId")//用户档案表
                    .addField("UAS.UserState AS userState")//员工状态

                    .addLeftJoin("TS_OrganizationInfo O ON O.OrgId = M.OrgId")//组织机构表
                    .addField("O.OrgName AS orgName")//所属公司
                    .setPbid(bid)
                    .queryMap();

            //DetailList detailList = new DetailList(itemList,entity.getId());
            //model.addAttribute("detail", detailList);
            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TB_UserMonthPerformanceReport, itemList);
            Integer onProcess = ysProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value ? 0 : 1;
            model.addAttribute("onProcess", onProcess);

            return commonApiService.toEdit(model,entity.getId(), new TbUserMonthPerformanceReport(), MenuId.TB_UserMonthPerformanceReport, extraParam, subGridConfig);

            //return "base/tbUserMonthPerformanceReport/toEdit";
        }catch (Exception e) {
            logger.error("详情页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }



}






